﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace weijiniya
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }



        private void label1_Click(object sender, EventArgs e)
        {
            //this.label1.Text = "请输入明文";
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.label1.Text = "请输入明文";
            this.label2.Text = "请输入密钥";
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            string PlainText = this.textBox1.Text;
            string Key = this.textBox2.Text;
            string CipherText=null ;
            int numkey;
            //listnum储存key的整数形式
            List<int> listnum = new List<int>();
            List<int> listkey = new List<int>();
            for (int i = 0; i < Key.Length; i++)
            {
                numkey = Key[i] - 'a';
                listnum.Add(numkey);
            }

            for (int i = 0; i < PlainText.Length; i++)
            {
                //明文字母转换为整数形式
                int numplain = PlainText[i] - 'a';
                //计算密文字母的整数形式
                int n = (i % Key.Length);
                int numcipher = numplain + listnum[n];
                //密文转换为字母形式
                numcipher %= 26;              
                CipherText += Convert.ToChar(numcipher + 'a');
            }
            this.label3.Text = CipherText;
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}
